package com.pj.project.sys_dict;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pj.project.sys_dict.entity.SysDictData;
import com.pj.project.sys_dict.entity.SysDictType;
import com.pj.project.sys_dict.mapper.SysDictDataMapper;
import com.pj.project.sys_dict.service.ISysDictDataService;
import com.pj.project.sys_dict.service.ISysDictTypeService;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController
{
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @PostMapping("/list")
    public AjaxJson list() {
        SoMap so = SoMap.getRequestSoMap();
        List<SysDictType> list = dictDataMapper.getList(so.startPage());

        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 查询字典数据详细
     */
    @PostMapping(value = "/{dictCode}")
    public AjaxJson getInfo(@PathVariable Long dictCode)
    {
        return AjaxJson.getSuccessData(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @PostMapping(value = "/type/{dictType}")
    public AjaxJson dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if ( null == data || data.size()==0)
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxJson.getSuccessData(data);
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public AjaxJson add(@Validated  SysDictData dict)
    {

        int i = dictDataService.insertDictData(dict);

        if(i==0){
            return AjaxJson.getError("新增字典'" + dict.getDictLabel() + "'失败，字典类型已存在");
        }
        
        return AjaxJson.getSuccessData(i);
    }

    /**
     * 修改保存字典类型
     */
    @PostMapping("/update")
    public AjaxJson edit(@Validated  SysDictData dict)
    {

        int i = dictDataService.insertDictData(dict);

        if(i==0){
            return AjaxJson.getError("修改字典'" + dict.getDictLabel() + "'失败，字典类型已存在");
        }

        return AjaxJson.getSuccessData(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @PostMapping("/del/{dictCode}")
    public AjaxJson remove(@PathVariable Long dictCode)
    {
        dictDataService.deleteDictDataByIds(dictCode);
        return AjaxJson.getSuccess();
    }
}
